package com.huike.report.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huike.business.domain.TbBusiness;
import com.huike.business.service.TbBusinessService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.service.TbClueService;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.domain.TbContract;
import com.huike.contract.service.TbContractService;
import com.huike.report.domain.vo.BusinessChangeStatisticsVO;
import com.huike.report.domain.vo.IndexBaseInfoVO;
import com.huike.report.domain.vo.IndexTodayInfoVO;
import com.huike.report.domain.vo.IndexTodoInfoVO;
import com.huike.report.dto.BusinessChangeStatisticsDTO;
import com.huike.report.dto.BusinessChangeStatisticsSQLDTO;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IndexService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class IndexServiceImpl implements IndexService {

    private final ReportMapper reportMapper;

    private final TbClueService clueService;

    private final TbBusinessService businessService;

    private final TbContractService contractService;

    @Override
    public List<BusinessChangeStatisticsVO> getBusinessChangeStatistics(BusinessChangeStatisticsDTO businessChangeStatisticsDTO) {
        List<BusinessChangeStatisticsSQLDTO> businessChangeStatisticsSQLDTO = reportMapper.getDate(businessChangeStatisticsDTO);
        log.info(businessChangeStatisticsSQLDTO.toString());
        List<BusinessChangeStatisticsVO> businessChangeStatisticsVOs = new ArrayList<>();
        businessChangeStatisticsSQLDTO.forEach(businessChangeStatistics -> {
            BusinessChangeStatisticsVO businessChangeStatisticsVO = new BusinessChangeStatisticsVO();
            businessChangeStatisticsVO.setCreate_by(businessChangeStatistics.getCreateBy());
            businessChangeStatisticsVO.setDeptName(businessChangeStatistics.getDeptName());
            businessChangeStatisticsVO.setNum(businessChangeStatistics.getCount());
            businessChangeStatisticsVO.setRadio(new BigDecimal(businessChangeStatistics.getCount())
                    .divide(new BigDecimal(businessChangeStatistics.getAllCount()), 4 , RoundingMode.HALF_UP));
            businessChangeStatisticsVOs.add(businessChangeStatisticsVO);
        });
        businessChangeStatisticsVOs.sort((o1, o2) -> o2.compareTo(o1.getRadio()));
        return businessChangeStatisticsVOs;
    }

    @Override
    public List<BusinessChangeStatisticsVO> getSalesStatistic(BusinessChangeStatisticsDTO businessChangeStatisticsDTO) {
        List<BusinessChangeStatisticsSQLDTO> businessChangeStatisticsSQLDTO = reportMapper.getSalesStatisticDate(businessChangeStatisticsDTO);
        List<BusinessChangeStatisticsVO> businessChangeStatisticsVOs = new ArrayList<>();
        businessChangeStatisticsSQLDTO.forEach(businessChangeStatistics -> {
            BusinessChangeStatisticsVO businessChangeStatisticsVO = new BusinessChangeStatisticsVO();
            businessChangeStatisticsVO.setCreate_by(businessChangeStatistics.getCreateBy());
            businessChangeStatisticsVO.setDeptName(businessChangeStatistics.getDeptName());
            businessChangeStatisticsVO.setNum(businessChangeStatistics.getCount());
            businessChangeStatisticsVO.setRadio(new BigDecimal(businessChangeStatistics.getCount())
                    .divide(new BigDecimal(businessChangeStatistics.getAllCount()), 4 , RoundingMode.HALF_UP));
            businessChangeStatisticsVOs.add(businessChangeStatisticsVO);
        });
        businessChangeStatisticsVOs.sort((o1, o2) -> o2.compareTo(o1.getRadio()));
        return businessChangeStatisticsVOs;
    }

    @Override
    public IndexTodayInfoVO getTodayInfo(BusinessChangeStatisticsDTO businessChangeStatisticsDTO) {
        String starTime = LocalDateTime.of(LocalDate.now() , LocalTime.MIN).toString();
        String endTime = LocalDateTime.now().toString();

        LambdaQueryWrapper<TbClue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.gt(TbClue::getCreateTime , starTime);
        lambdaQueryWrapper1.lt(TbClue::getCreateTime ,endTime);
        List<TbClue> list1 = clueService.list(lambdaQueryWrapper1);

        LambdaQueryWrapper<TbBusiness> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.gt(TbBusiness::getCreateTime , starTime);
        lambdaQueryWrapper2.lt(TbBusiness::getCreateTime , endTime);
        List<TbBusiness> list2 = businessService.list(lambdaQueryWrapper2);

        LambdaQueryWrapper<TbContract> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.gt(TbContract::getCreateTime , starTime);
        lambdaQueryWrapper3.lt(TbContract::getCreateTime , endTime);
        List<TbContract> list3 = contractService.list(lambdaQueryWrapper3);

        Double sum = contractService.getSum(starTime , endTime);
        if (sum == null){
            sum = 0.0;
        }

        IndexTodayInfoVO indexTodayInfoVO = new IndexTodayInfoVO();
        indexTodayInfoVO.setTodayCluesNum(list1.size());
        indexTodayInfoVO.setTodayBusinessNum(list2.size());
        indexTodayInfoVO.setTodayContractNum(list3.size());
        indexTodayInfoVO.setTodaySalesAmount(sum);
        return indexTodayInfoVO;
    }

    @Override
    public IndexBaseInfoVO getBaseInfo(BusinessChangeStatisticsDTO businessChangeStatisticsDTO) {
        String starTime = businessChangeStatisticsDTO.getBeginCreateTime();
        String endTime = businessChangeStatisticsDTO.getEndCreateTime();

        LambdaQueryWrapper<TbClue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.gt(TbClue::getCreateTime , starTime);
        lambdaQueryWrapper1.lt(TbClue::getCreateTime ,endTime);
        List<TbClue> list1 = clueService.list(lambdaQueryWrapper1);

        LambdaQueryWrapper<TbBusiness> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.gt(TbBusiness::getCreateTime , starTime);
        lambdaQueryWrapper2.lt(TbBusiness::getCreateTime , endTime);
        List<TbBusiness> list2 = businessService.list(lambdaQueryWrapper2);

        LambdaQueryWrapper<TbContract> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.gt(TbContract::getCreateTime , starTime);
        lambdaQueryWrapper3.lt(TbContract::getCreateTime , endTime);
        List<TbContract> list3 = contractService.list(lambdaQueryWrapper3);

        Double sum = contractService.getSum(starTime , endTime);
        if (sum == null){
            sum = 0.0;
        }

        IndexBaseInfoVO indexBaseInfoVO = new IndexBaseInfoVO();
        indexBaseInfoVO.setCluesNum(list1.size());
        indexBaseInfoVO.setBusinessNum(list2.size());
        indexBaseInfoVO.setContractNum(list3.size());
        indexBaseInfoVO.setSalesAmount(sum);
        return indexBaseInfoVO;
    }

    @Override
    public IndexTodoInfoVO getTodoInfo(BusinessChangeStatisticsDTO businessChangeStatisticsDTO) {
        String starTime = businessChangeStatisticsDTO.getBeginCreateTime();
        String endTime = businessChangeStatisticsDTO.getEndCreateTime();

        LambdaQueryWrapper<TbClue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.gt(TbClue::getCreateTime , starTime);
        lambdaQueryWrapper1.lt(TbClue::getCreateTime ,endTime);
        lambdaQueryWrapper1.eq(TbClue::getCreateBy , SecurityUtils.getUsername());
        lambdaQueryWrapper1.eq(TbClue::getStatus , 1);
        List<TbClue> list1 = clueService.list(lambdaQueryWrapper1);

        LambdaQueryWrapper<TbBusiness> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.gt(TbBusiness::getCreateTime , starTime);
        lambdaQueryWrapper2.lt(TbBusiness::getCreateTime , endTime);
        lambdaQueryWrapper2.eq(TbBusiness::getCreateBy , SecurityUtils.getUsername());
        lambdaQueryWrapper2.eq(TbBusiness::getStatus , 1);
        List<TbBusiness> list2 = businessService.list(lambdaQueryWrapper2);

        IndexTodoInfoVO indexTodoInfoVO = new IndexTodoInfoVO();
        indexTodoInfoVO.setToallocatedCluesNum(list1.size());
        indexTodoInfoVO.setToallocatedBusinessNum(list2.size());
        indexTodoInfoVO.setTofollowedCluesNum(0);
        indexTodoInfoVO.setTofollowedBusinessNum(0);
        return indexTodoInfoVO;
    }
}
